package edu.thu.studio.ormdesigner.outline.editpart;

import java.util.Collections;
import java.util.List;

import org.eclipse.gef.DragTracker;
import org.eclipse.gef.Request;
import org.eclipse.gef.RequestConstants;
import org.eclipse.gef.tools.SelectEditPartTracker;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.ui.PlatformUI;

import edu.thu.studio.ormdesigner.Activator;
import edu.thu.studio.ormdesigner.model.domain.Domain;
import edu.thu.studio.ormdesigner.outline.command.UpdateDomainCommand;
import edu.thu.studio.ormdesigner.outline.dialog.DomainDialog;
import edu.thu.studio.ormdesigner.util.Properties;

public class DomainOutlineEditPart extends AbstractOutlineEditPart {
    
    @Override
    @SuppressWarnings("rawtypes")
    protected List getModelChildren() {
        return Collections.emptyList();
    }
    
    /**
     * {@inheritDoc}
     */
    @Override
    protected void refreshOutlineVisuals() {
        Domain model = (Domain) this.getModel();
        
        this.setWidgetText(model.getName());
        this.setWidgetImage(Activator.getImage(Properties.icon_domain));
    }
    
    /**
     * {@inheritDoc}
     */
    @Override
    public DragTracker getDragTracker(Request req) {
        return new SelectEditPartTracker(this);
    }
    
    public boolean isDeleteable() {
        return true;
    }
    
    @Override
    public void performRequest(Request request) {
        if (RequestConstants.REQ_OPEN.equals(request.getType())) {
            Domain domain = (Domain) this.getModel();
            DomainDialog dialog = new DomainDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),
                domain, this.getORMModel());
            if (dialog.open() == IDialogConstants.OK_ID) {
                UpdateDomainCommand command = new UpdateDomainCommand(this.getORMModel(), domain, dialog.getResult());
                this.execute(command);
            }
        }
    }
}
